package com.yyfax.pay.third.model.to.yyfax;

import java.util.Date;

import com.yyfax.pay.common.enums.OrderStateEnum;
import com.yyfax.pay.common.exception.PayExceptionCode;

/**
 * 数据绑定基类
 * 
 * @author buyi
 * @date 2018-05-12 04:00:02
 * @since v2.1.5
 */
public abstract class DataBinder {

	/**
	 * 异常信息
	 */
	private Exception exception;
	/**
	 * 请求时间
	 */
	private Date reqDate;

	/**
	 * 支付模块业务主键
	 */
	private String busiId;
	
	/**
	 * 支付公司返回的流水号
	 */
	private String serialNo;

	/**
	 * 请求路径
	 */
	private String reqUrl;
	/**
	 * 请求内容
	 */
	private String reqContent;
	
	/**
	 * 响应内容
	 */
	private String rspContent;

	/**
	 * 支付响应码
	 */
	private Integer resultCode;
	
	/**
	 * 支付响应描述
	 */
	private String resultMsg;

	public Exception getException() {
		return exception;
	}

	public void setException(Exception exception) {
		this.exception = exception;
	}

	public Date getReqDate() {
		return reqDate;
	}

	public void setReqDate(Date reqDate) {
		this.reqDate = reqDate;
	}

	public String getBusiId() {
		return busiId;
	}

	public void setBusiId(String busiId) {
		this.busiId = busiId;
	}

	public String getSerialNo() {
		return serialNo;
	}

	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}

	public String getReqUrl() {
		return reqUrl;
	}

	public void setReqUrl(String reqUrl) {
		this.reqUrl = reqUrl;
	}

	public String getReqContent() {
		return reqContent;
	}

	public void setReqContent(String reqContent) {
		this.reqContent = reqContent;
	}

	public String getRspContent() {
		return rspContent;
	}

	public void setRspContent(String rspContent) {
		this.rspContent = rspContent;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public String getResultMsg() {
		return resultMsg;
	}

	public void setResultMsg(String resultMsg) {
		this.resultMsg = resultMsg;
	}

	/**
	 * 获取订单状态<br>
	 * 转换响应码为{@link com.yyfax.pay.common.enums.OrderStateEnum}
	 * 
	 * @author buyi
	 * @date 2018-05-12 03:37:41
	 * @since v2.1.5
	 * @return
	 */
	public OrderStateEnum getState() {
		if (exception == null && PayExceptionCode.SUCCESS.isCode(getResultCode())) {
			// 如果响应码为成功，则返回state为成功
			return OrderStateEnum.SUCCESS;
		}

		if (PayExceptionCode.isTimeout(getResultCode())) {
			// 如果响应码为超时，则返回超时状态
			return OrderStateEnum.TIMEOUT;
		}

		if (PayExceptionCode.isToConfirm(getResultCode())) {
			// 如果响应码为待确认类型的，则返回待确认状态
			return OrderStateEnum.TOCONFIRM;
		}

		// 其他情况，则默认返回失败
		return OrderStateEnum.FAIL;
	}

	/**
	 * 是否异常
	 * 
	 * @author buyi
	 * @date 2018-05-12 01:30:59
	 * @since v2.1.5
	 * @return true-出现异常
	 */
	public boolean isError() {
		return exception != null;
	}

	/**
	 * 设置结果
	 * 
	 * @author buyi
	 * @date 2018-05-12 02:28:36
	 * @since v2.1.5
	 * @param resultCode
	 * @param resultMsg
	 */
	public void setResult(Integer resultCode, String resultMsg) {
		setResultCode(resultCode);
		setResultMsg(resultMsg);
	}

	/**
	 * 设置结果
	 * 
	 * @author buyi
	 * @date 2018-05-12 02:28:48
	 * @since v2.1.5
	 * @param code
	 */
	public void setResult(PayExceptionCode code) {
		setResultCode(code.getCode());
		setResultMsg(code.getDesc());
	}

}
